/**
 * Created by Felix on 2017/8/2.
 */
'use strict';

salesCtrl.controller('orderPriceCtrl', ['$rootScope', '$scope', '$http', '$location', function ($rootScope, $scope, $http, $location) {
    //配置
    $scope.count = 1; //数据总数
    $scope.p_pernum = 10; //每页显示条数
    //变量
    $scope.p_current = 1;  //当前页数
    $scope.p_all_page = 0;  //总页数
    $scope.pages = [];

    $scope.departProvinceCode =null;     //起点省
    $scope.departCityCode =null;    //起点市
    $scope.departCityList =null;    //起点市
    $scope.departCountyCode =null;  //起点区
    $scope.departCountyList =null;  //起点区
    $scope.receiptProvinceCode =null;    //目的地
    $scope.receiptCityCode =null;   //目的市
    $scope.receiptCityList =null;   //目的市
    $scope.receiptCountyCode =null; //目的区
    $scope.receiptCountyList =null; //目的区
    $scope.consignorId =null; //客户
    $scope.status =null;    //订单状态
    $scope.billDateStart =null;   //下单日期
    $scope.billDateEnd =null; //下单日期
    $scope.orderCodes =null; //订单编号
    //初始化第一页
    getPage($scope.p_current,$scope.p_pernum,function(){
        //alert("我是第一次加载");
    });
    $scope.seaBtn = function () {
        getPage($scope.p_current, $scope.p_pernum, "1")
    };
    function getPage(page,size,callback){
        $http.post("adjust/order-list",{
            "pageNo":page,
            "departProvinceCode":$scope.departProvinceCode,
            "departCityCode":$scope.departCityCode,
            "departCountyCode":$scope.departCountyCode,
            "receiptProvinceCode":$scope.receiptProvinceCode,
            "receiptCityCode":$scope.receiptCityCode,
            "receiptCountyCode":$scope.receiptCountyCode,
            "consignorId":$scope.consignorId,
            "status":$scope.status,
            "billDateStartString":$scope.billDateStart,
            "billDateEndString":$scope.billDateEnd,
            "orderCodes":$scope.orderCodes
        }).success(function(res){

            // var thisData= res.data.concat().splice(page*10-10, 10);//当前页数*每页显示条数-每页显示条数,每页显示条数
            /*     if(res&&res.status==1){ */
            var thisData= res.data;
            $scope.count=res.data.page.totalRecord;
            $scope.data=thisData;
            $scope.p_current = page;
            $scope.p_all_page =Math.ceil($scope.count/$scope.p_pernum);
            reloadPno();//第一次调用并加载
            //callback();
            /* }else{
             alert("加载失败");
             }  */
        });
    }
    //单选按钮选中
    $scope.select= function(id){
        // console.log(id);
    };
    //首页
    $scope.p_index = function(){
        $scope.load_page(1);
    };
    //尾页
    $scope.p_last = function(){
        $scope.load_page($scope.p_all_page);
    };
    //上一页
    $scope.prev_page=function(e){
        if(e.p_current==0){
            return;
        }
        getPage(e.p_current-1,$scope.p_pernum,function(){});
    };
    //下一页
    $scope.next_page=function(e){
        if(e.p_current==$scope.p_all_page){
            return;
        }
        getPage(e.p_current+1,$scope.p_pernum,function(){});
    };

    //加载某一页
    $scope.load_page = function(page){
        getPage(page,$scope.p_pernum,function(){});
    };
    //跳转到指定页
    $scope.goBtn =function(){
        var idx=$("#on").val();
        getPage(idx,$scope.p_pernum,function(){});
    };
    //初始化页码
    var reloadPno = function(){
        $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);
    };

    var calculateIndexes = function (current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };

    //重置
    $scope.resetBtn=function(){
        $scope.departProvinceCode =null;     //起点省
        $scope.departCityCode =null;    //起点市
        $scope.departCityList =null;    //起点市
        $scope.departCountyCode =null;  //起点区
        $scope.departCountyList =null;  //起点区
        $scope.receiptProvinceCode =null;    //目的地
        $scope.receiptCityCode =null;   //目的市
        $scope.receiptCityList =null;   //目的市
        $scope.receiptCountyCode =null; //目的区
        $scope.receiptCountyList =null; //目的区
        $scope.consignorId =null; //客户
        $scope.status =null;    //订单状态
        $scope.billDateStart =null;   //下单日期
        $scope.billDateEnd =null; //下单日期
        $scope.orderCodes =null; //客户订单号
        $(".valName").val("");
    };

    $scope.adjustBtn=function(){
        var id = this.x.id;
        var orderCost=this.x.orderCost;//订单金额
        var distance=this.x.distance;//距离
        layer.open({
            type: 2,
            title:'价格调整',
            area: ['450px', '450px'], //宽高
            content:"index/costAndDistance?orderCost="+orderCost+"&distance="+distance,//传参数：价格、距离。
            btn:["确定","取消"],
            btnAlign: 'c', //按钮居中
            yes:function(index, layero){
                layer.close(index);
                //获取跳转页面的值：订单价，调整金额，结算价，调整页面
                var orderCost = $("#orderCost").val();
                var aftCost = $("#aftCost").val();
                var distance = $("#distance").val();
                var aftDistance = $("#aftDistance").val();
                var comment = $("#comment").val();
                $http.post("adjust/update-cost",{
                    "orderCost":orderCost,
                    "aftCost":aftCost,
                    "distance":distance,
                    "aftDistance":aftDistance,
                    "comment":comment,
                    "id":id
                }).success(function(res){
                    if(res.success) {
                        getPage($scope.p_current, $scope.p_pernum, function () {});
                        layer.msg('调整成功！');
                    }else{
                        layer.msg(res.message);
                    }
                });
            }
        });
    };

    //全选
    $scope.selectAll=false;
    var isAllSelect=true;
    $scope.all= function (m) {
        for(var i=0;i<$scope.names.length;i++){
            if(m===true){
                $scope.names[i].state=true;
                alert("全选");
            }else {
                $scope.names[i].state=false;
                alert("取消全选");
            }
        }

    };
    //是否开发票
    $scope.orderOptions = [
        {
            id: 10,
            name: "待接单"
        },
        {
            id: 30,
            name: "待支付"
        },
        {
            id: 40,
            name: "执行中"
        },
        {
            id: 50,
            name: "已提车"
        },
        {
            id: 60,
            name: "已发车"
        },
        {
            id: 70,
            name: "运输中"
        },
        {
            id: 80,
            name: "已交车"
        },
        {
            id: 90,
            name: "已完成"
        }
    ];
    //起点地选择省份
    $scope.code = function(){
        $http.post("barea/by-parentcode", {
            "parentCode": $scope.departProvinceCode
        }).success(function (res) {
            $scope.departCityList = res.data;
            $scope.departCountyList = null;
            $scope.departCityCode = null;
        });
    };
    //起点地选择城市
    $scope.dcity = function(){
        $http.post("barea/by-parentcode", {
            "parentCode": $scope.departCityCode
        }).success(function (res) {
            $scope.departCountyList = res.data;
            $scope.departCountyCode = null;
        });
    };
    //目的地选择省份
    $scope.rcode = function(){
        $http.post("barea/by-parentcode", {
            "parentCode": $scope.receiptProvinceCode
        }).success(function (res) {
            $scope.receiptCityList = res.data;
            $scope.receiptCountyList = null;
            $scope.receiptCityCode = null;
        });
    };
    //目的地选择城市
    $scope.rcity = function(){
        $http.post("barea/by-parentcode", {
            "parentCode": $scope.receiptCityCode
        }).success(function (res) {
            $scope.receiptCountyList = res.data;
            $scope.receiptCountycode = null;
        });
    };

    //获取开始日期
    $scope.getTime=function($event,str){
        $scope.billDateStart =$scope.billDateStart == null? $scope.billDateStart = getNowFormatDate():$scope.billDateStart =$scope.billDateStart;
        layui.laydate({
            elem:$event.target,
            istime: false,//是否开启时间选择
            istoday: true,//是否显示今天
            isclear: true,//是否显示清空
            festival: true, //显示节日
            format:'YYYY-MM-DD',//日期格式
            choose:function(date){
                $scope[str]=date;
            }
        })
    };
    //获取截取日期
    $scope.getTimeEnd=function($event,str){
        $scope.billDateEnd =$scope.billDateEnd == null? $scope.billDateEnd = getNowFormatDate():$scope.billDateEnd =$scope.billDateEnd;
        layui.laydate({
            elem:$event.target,
            istime: false,//是否开启时间选择
            istoday: true,//是否显示今天
            isclear: true,//是否显示清空
            festival: true, //显示节日
            format:'YYYY-MM-DD',//日期格式
            choose:function(date){
                $scope[str]=date;
            }
        })
    };
    //获取当前的时间
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
}]);

